package com.party.core.service.picCloud;

import com.alibaba.fastjson.JSONObject;
import com.party.core.utils.MyBeanUtils;
import com.party.file.cos.COSClient;
import com.party.file.cos.ClientConfig;
import com.party.file.cos.common_utils.CommonFileUtils;
import com.party.file.cos.dto.UploadResult;
import com.party.file.cos.meta.InsertOnly;
import com.party.file.cos.request.DelFileRequest;
import com.party.file.cos.request.UploadFileRequest;
import com.party.file.cos.sign.Credentials;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.InputStream;
import java.nio.charset.Charset;

/**
 * @author： yifeng
 * @description： Cos
 * @date：Created in  2018/01/08 下午 13:54
 **/
@Service
public class CosCloudBizService {
    private final int APP_ID = 10052192;
    private final String SECRET_ID = "AKID0zLb467fRifqb0rsyo5tVJKZqKiebe5B";
    private final String SECRET_KEY = "UcfDJpkyGi90IDw0YsnxxUcLkWopbrC6";
    private final String BUCKET = "tongxingzhe";
    private final String REGION = "sh";
    private COSClient cosClient = null;

    @PostConstruct
    public void getCosClient() {
        if (cosClient == null) {
            // 初始化客户端配置
            ClientConfig clientConfig = new ClientConfig();
            // 设置bucket所在的区域，比如广州(gz), 天津(tj)
            clientConfig.setRegion(REGION);
            // 初始化秘钥信息
            Credentials cred = new Credentials(APP_ID, SECRET_ID, SECRET_KEY);
            // 初始化cosClient
            cosClient = new COSClient(clientConfig, cred);
        }
    }

    /**
     * 上传文件
     *
     * @param inputStream 文件流
     * @param cosFilePath 文件目录
     * @return
     * @throws Exception
     */
    public UploadResult uploadFile(InputStream inputStream, String cosFilePath, InsertOnly insertOnly) throws Exception {
        byte[] contentBuffer = CommonFileUtils.getFileContent(inputStream, 0L, inputStream.available())
                .getBytes(Charset.forName(("ISO-8859-1")));
        UploadFileRequest overWriteFileRequest = new UploadFileRequest(BUCKET, cosFilePath, contentBuffer);
        overWriteFileRequest.setInsertOnly(insertOnly);
        String overWriteFileRet = cosClient.uploadFile(overWriteFileRequest);
        UploadResult uploadResult = JSONObject.parseObject(overWriteFileRet, UploadResult.class);
        if (uploadResult.getCode() == 0 && "SUCCESS".equals(uploadResult.getMessage())) {
            String dataStr = uploadResult.getData();
            UploadResult result = JSONObject.parseObject(dataStr, UploadResult.class);
            MyBeanUtils.copyBeanNotNull2Bean(result, uploadResult);
        }
        return uploadResult;
    }

    /**
     * 删除文件
     *
     * @param cosFilePath 文件目录
     * @return
     * @throws Exception
     */
    public UploadResult deleteFile(String cosFilePath) {
        DelFileRequest delFileRequest = new DelFileRequest(BUCKET, cosFilePath);
        String delFileRet = cosClient.delFile(delFileRequest);
        return JSONObject.parseObject(delFileRet, UploadResult.class);
    }
}
